package com.etalk.crm.controller.lesson;

import com.alibaba.fastjson.JSON;
import com.etalk.crm.pojo.ClassChangeAutoRecord;
import com.etalk.crm.service.ClassChangeAutoRecordService;
import com.etalk.crm.utils.PojoMapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Jordan
 * @Date 2020/12/15 14:21
 * @Version 1.0
 */

@Controller
@RequestMapping("/class")
public class ClassChangeAutoRecordController {

    @Resource
    private ClassChangeAutoRecordService classChangeAutoRecordService;


    @RequestMapping(value = "/change/auto/page",method = {RequestMethod.GET,RequestMethod.POST})
    public String getListByPage(Model model,ClassChangeAutoRecord record, Integer pageNum, Integer pageSize){

        model.addAllAttributes(PojoMapUtils.convert2Map(record));
        model.addAttribute("pageInfo",classChangeAutoRecordService.searchClassChangeAutoRecordListByPage(record, pageNum, pageSize));

        return "lesson/class_change_auto_list";
    }

    /**
     * 添加自动调课计划
     * @param lessonId 课程id
     * @param newReleaseTime 新课程时间
     * @param reasonId 调课原因
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/change/auto/add/{lessonId}",produces = "application/json; charset=utf-8")
    public String setClassChangeInfo(@PathVariable("lessonId")Integer lessonId,String newReleaseTime,@RequestParam Integer reasonId){
        Map<String,Object> resultMap = classChangeAutoRecordService.addClassChangeAutoRecord(lessonId,newReleaseTime,reasonId);

        return JSON.toJSONString(resultMap);
    }

    /**
     * 批量增加调课计划
     * @param lessonIds 多个课程id，以英文逗号隔开
     * @param reasonId 调课原因
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/change/auto/batch/add",produces = "application/json; charset=utf-8")
    public String setBatchClassChangeInfo(@RequestParam("lessonId") String lessonIds, @RequestParam Integer reasonId){
        List<Integer> listLessonId=new ArrayList<>();
        if (StringUtils.isNotEmpty(lessonIds)){
            for (String id:lessonIds.split(",")){
                if (StringUtils.isNotEmpty(id)){
                    listLessonId.add(Integer.parseInt(id));
                }
            }
        }
        Map<String,Object> resultMap = classChangeAutoRecordService.batchAddClassChangeAutoRecord(listLessonId,reasonId);

        return JSON.toJSONString(resultMap);
    }

    /**
     * 取消自动调课记录
     * @param id 调课记录id
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/change/auto/cancel/{id}",produces = "application/json; charset=utf-8")
    public String cancelClassChangeInfo(@PathVariable("id") int id){

        Map<String,Object> resultMap = classChangeAutoRecordService.cancelClassChangeAutoRecordById(id);

        return JSON.toJSONString(resultMap);
    }

    /**
     * 批量取消自动调课记录
     * @param listId 调课记录id
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/change/auto/batch/cancel",produces = "application/json; charset=utf-8")
    public String cancelBatchClassChangeInfo(@RequestParam("listId") List<Integer> listId){

        Map<String,Object> resultMap = classChangeAutoRecordService.cancelClassChangeAutoRecordById(listId);

        return JSON.toJSONString(resultMap);
    }

}
